<template>
<div>
    <input type="file" @change="changeFile" placeholder="选择文件" ref="file1">
    <el-button type="primary" @click="uploadFile">上 传</el-button>
    <br>
    <el-button type="primary" @click="apiTest">接口测试1</el-button>
    <el-button type="primary" @click="apiTest2">接口测试2</el-button>
</div>
</template>

<script setup>
import OSS from "ali-oss";
import axios from "axios";
import {ref} from "vue";

const file1 = ref(null)

let chooseFile = null

const changeFile = (e) => {
    console.log("选中文件: ", e.target.files[0])
    chooseFile = e.target.files[0]
}

const uploadFile = () => {
    if (!chooseFile) {
        console.log("请上传文件")
        return
    }
    let filename = chooseFile.name
    let arr = filename.split('.')
    let ext = arr.length > 1 ?  arr.pop() : ''
    console.log(ext)
    axios.post('http://frp.psn.asia/admin/test/getStsToken',{}).then(res => {
        // console.log("getStsToken 成功:")
        // console.log(res.data)
        let client = new OSS({
            region: 'oss-cn-beijing',
            accessKeyId: res.data.res.AccessKeyId,
            accessKeySecret: res.data.res.AccessKeySecret,
            bucket: res.data.bucket,
            stsToken: res.data.res.SecurityToken,
            refreshSTSToken: () => {
                console.log("refreshSTSToken()过期啦...")
            }
        });

        return client.put( "tmp/rani" + '.' + ext, chooseFile).then(res =>{
            console.log("client.put 成功:")
            console.log(res)
            chooseFile = null
            file1.value.value = ""
        })
    }).catch(err => {
        console.log(err,'---get token failed')
    })
}

const apiTest = () => {
    axios.post('http://frp.psn.asia/admin/test/test',{}).then(res => {
        console.log("test/test1 成功:")
        console.log(res.data)
    }).catch(err => {
        console.log(err,'---get token failed')
    })
}

const apiTest2 = () => {
    axios.post('http://frp.psn.asia/admin/test/getStsToken',{}).then(res => {
        console.log("getStsToken 成功:")
        console.log(res.data)
    }).catch(err => {
        console.log(err,'---get token failed')
    })
}
</script>

<style scoped>

</style>